package com.noah.project;


import com.noah.project.model.Reply;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
public class GlobalExceptionHandler {
    private Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 系统异常处理，比如：404,500
     *
     * @param req
     * @param e
     * @return
     * @throws Exception
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Reply defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
        logger.error("", e);
        Reply reply = new Reply();
        reply.setData(e.getMessage());
        if (e instanceof org.springframework.web.servlet.NoHandlerFoundException) {
            reply.setCode(-404);
            reply.setMsg("找不到服务器");
        } else {
            reply.setCode(-500);
            reply.setMsg("服务器异常");
        }
        return reply;
    }
}